package cn.zzz.gen;

import java.util.*;

public class Table {
    public Table() {
    }

    public Table(String name, List<Column> columns) {
        this.name = name;
        this.columns = columns;
    }

    private String name;
    private List<Column> columns;

    public String tablePrefix() {
        return name.indexOf("_") == -1 || name.indexOf("_") >= 3 ? GenConfig.tableDefaultPrefix : name.substring(0, name.indexOf("_"));
    }

    public String tablePrefixAndDot() {
        if (GenConfig.tablePrefix) {
            return "." + tablePrefix();
        } else {
            return "";
        }

    }

    public String entityPackage() {
        return GenConfig.entityBasePackage + tablePrefixAndDot() + "." + GenConfig.entityPackageName;
    }

    public String entityPath() {
        return GenConfig.entityProjectPath + "/src/main/java/" + entityPackage().replaceAll("\\.", "/");
    }

    public String entityClassName() {
        return GenConfig.entityNamePrefix + CamelUtils.underlineToCamel2(name);
    }

    public String entityFile() {
        return entityPath() + "/" + entityClassName() + ".java";
    }

    public String entityClassNameAll() {
        return entityPackage() + "." + entityClassName();
    }

    //
    public String entityTblPackage() {
        return GenConfig.entityBasePackage + tablePrefixAndDot() + "." + GenConfig.entityTblPackageName;
    }

    public String entityTblPath() {
        return GenConfig.entityProjectPath + "/src/main/java/" + entityTblPackage().replaceAll("\\.", "/");
    }

    public String entityTblClassName() {
        return entityClassName() + GenConfig.entityTblNamePostfix;
    }

    public String entityTblFile() {
        return entityTblPath() + "/" + entityTblClassName() + ".java";
    }

    //
    public String daoPackage() {
        return GenConfig.daoBasePackage + tablePrefixAndDot() + "." + GenConfig.daoPackageName;
    }

    public String daoPath() {
        return GenConfig.daoProjectPath + "/src/main/java/" + daoPackage().replaceAll("\\.", "/");
    }

    public String daoClassName() {
        return GenConfig.daoNamePrefix + CamelUtils.underlineToCamel2(name) + GenConfig.daoNamePostfix;
    }

    public String daoFile() {
        return daoPath() + "/" + daoClassName() + ".java";
    }

    public String daoClassNameAll() {
        return daoPackage() + "." + daoClassName();
    }

    //
    public String dao2Package() {
        return GenConfig.dao2BasePackage + tablePrefixAndDot() + "." + GenConfig.dao2PackageName;
    }

    public String dao2Path() {
        return GenConfig.daoProjectPath + "/src/main/java/" + dao2Package().replaceAll("\\.", "/");
    }

    public String dao2ClassName() {
        return GenConfig.dao2NamePrefix + CamelUtils.underlineToCamel2(name) + GenConfig.dao2NamePostfix;
    }

    public String dao2File() {
        return dao2Path() + "/" + dao2ClassName() + ".java";
    }

    public String dao2ClassNameAll() {
        return dao2Package() + "." + dao2ClassName();
    }

    //
    public String servicePackage() {
        return GenConfig.serviceBasePackage + tablePrefixAndDot() + "." + GenConfig.servicePackageName;
    }

    public String servicePath() {
        return GenConfig.serviceProjectPath + "/src/main/java/" + servicePackage().replaceAll("\\.", "/");
    }

    public String serviceClassName() {
        return GenConfig.serviceNamePrefix + CamelUtils.underlineToCamel2(name) + GenConfig.serviceNamePostfix;
    }

    public String serviceFile() {
        return servicePath() + "/" + serviceClassName() + ".java";
    }

    public String serviceClassNameAll() {
        return servicePackage() + "." + serviceClassName();
    }

    //
    public String serviceImplPackage() {
        return GenConfig.serviceBasePackage + tablePrefixAndDot() + "." + GenConfig.serviceImplPackageName;
    }

    public String serviceImplPath() {
        return GenConfig.serviceProjectPath + "/src/main/java/" + serviceImplPackage().replaceAll("\\.", "/");
    }

    public String serviceImplClassName() {
        return GenConfig.serviceNamePrefix + CamelUtils.underlineToCamel2(name) + GenConfig.serviceImplNamePostfix;
    }

    public String serviceImplFile() {
        return serviceImplPath() + "/" + serviceImplClassName() + ".java";
    }

    public String serviceImplClassNameAll() {
        return serviceImplPackage() + "." + serviceImplClassName();
    }

    //
    public Column keyCol() {
        for (Column col : getColumns()) {
            if (col.isPK()) {
                return col;
            }
        }
        return null;
    }

    public String allColumnString() {
        StringBuffer all = new StringBuffer();
        for (Column col : getColumns()) {
            if (all.length() > 0) {
                all.append(",");
            }
            all.append("`");
            all.append(col.getField());
            all.append("`");
        }
        return all.toString();
    }

    public String allColumnValString() {
        StringBuffer all = new StringBuffer();
        for (Column col : getColumns()) {
            if (all.length() > 0) {
                all.append(",");
            }
            all.append("#{");
            all.append(col.getField());
            all.append(",jdbcType=" + col.jdbcType() + "}");
        }
        return all.toString();
    }

    //
    public Collection<Class<?>> importClazzes() {
        Set<Class<?>> clazzes = new TreeSet<>(new Comparator<Class<?>>() {
            @Override
            public int compare(Class o1, Class o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        });
        for (Column col : columns) {
            if (col.needImport()) {
                Class<?> clazz = col.javaType();
                clazzes.add(clazz);
            }
        }
        return clazzes;
    }


    //
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }
}
